package com.SMP.controller.class_room;
import java.util.List;

import com.SMP.model.News;
import com.SMP.model.Msg;
import com.SMP.service.class_room.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class NewsConfigController {
    /**
     * @author GJ
     */
    @Autowired
    NewsService newsService;
    /**
     * @author GJ
    //	 * @param 查询所有新闻
     * */
    @RequestMapping("/selAllNews")
    @ResponseBody
    public Msg selAllNews(@RequestParam(value="pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 5);
        List<News>list=newsService.selAllNews();
        PageInfo<News>page = new PageInfo<News>(list, 5);
        return Msg.success().add("pageInfo",page);
    }

    /**
     * @author GJ
    //	 * @param 根据标题查询搜索框的
     * */
    @RequestMapping("/titleNews" )
    @ResponseBody
    public Msg NewsTitle(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "title",defaultValue = "1")String title){
        PageHelper.startPage(pn, 5);
        List<News> news=newsService.NewsTitle(title);
        PageInfo page = new PageInfo(news, 5);
        return Msg.success().add("pageInfo",page);
    }
    /**
     * @author GJ
    //	 * @param 根据名字查询搜索框的
     * */
    @RequestMapping("/releasePeopleNews")
    @ResponseBody
    public Msg NewsReleasePeople(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "releasePeople",defaultValue = "1")String releasePeople){
        PageHelper.startPage(pn, 5);
        List<News> news=newsService.NewsReleasePeople(releasePeople);
        PageInfo page = new PageInfo(news, 5);
        return Msg.success().add("pageInfo",page);
    }
    /**
     * @author GJ
    //	 * @param 根据发布状态查询下拉框的
     * */
    @RequestMapping( "/releaseStateNews")
    @ResponseBody
    public Msg NewsReleaseState(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "releaseState",defaultValue = "1")String releaseState){
        PageHelper.startPage(pn, 5);
        List<News> news=newsService.NewsReleaseState(releaseState);
        PageInfo page = new PageInfo(news, 5);
        return Msg.success().add("pageInfo",page);
    }
    /**
     * @author GJ
    //	 * @param 根据信息类型查询下拉框的
     * */
    @RequestMapping("/newsType")
    @ResponseBody
    public Msg NewsType(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "newsType",defaultValue = "1")String newsType ){
        PageHelper.startPage(pn, 5);
        List<News> news=newsService.NewsType(newsType);
        PageInfo page = new PageInfo(news, 5);
        return Msg.success().add("pageInfo",page);
    }
        /**
//     * @author GJ
//    //	 * @param 根据时间类型查询
//     * */
    @RequestMapping("/releaseTime")
    @ResponseBody
    public Msg ReleaseTimeNews(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "releaseTime",defaultValue = "1")String releaseTime){
        PageHelper.startPage(pn, 5);
         List<News> news=newsService.ReleaseTimeNews(releaseTime);

        PageInfo page = new PageInfo(news, 5);
        return  Msg.success().add("pageInfo",page);
    }

    /**
     * @author GJ
    //	 * @param 根据id查询
     * */
    @RequestMapping(value="/selNews" ,method=RequestMethod.GET)
    @ResponseBody
    public Msg selectNewsId(String id){
        News news = newsService.selectNewsId(id);
        return Msg.success().add("news",news);
    }


//    /**
//     * @author GJ
//    //	 * @param 查询下拉框新闻
//     * */
//    @RequestMapping("/typeAllNews")
//    @ResponseBody
//
//    public Msg stateSelectAllNews(){
//        System.out.print("zoule");
//        List<News>list=newsService.stateSelectAllNews();
//        return Msg.success().add("state",list);
//    }
    /**
     * @author GJ
    //	 * @param 添加新闻
     * */
    @RequestMapping(value="/addNews")
    @ResponseBody
    public Msg addNews(News news){
        int a=newsService.addNews(news);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
    /**
     * @author GJ
    //	 * @param 发布
     * */
    @RequestMapping( value="/updState")
    @ResponseBody
    public Msg updReleaseState(News news){
        int a =newsService.updReleaseState(news);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
    /**
     * @author GJ
    //	 * @param 取消发布
     * */
    @RequestMapping( value="/updState1")
    @ResponseBody
    public Msg updReleaseState1(News news){
        int a =newsService.updReleaseState1(news);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
    /**
     * @author GJ
    //	 * @param 修改新闻
     * */
    @RequestMapping( value="/updNews")
    @ResponseBody
    public Msg updNews(News news){
        int a =newsService.updNews(news);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
    /**
     * @author GJ
    //	 * @param 删除新闻
     * */
    @RequestMapping(value="/delNews")
    @ResponseBody
    public Msg delNews(String id ){
        int a=newsService.delNewsF(id);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
}

